<template>
  <!-- 搜索页 顶部 -->
  <div class="ListViewTop">
    <div class="nav">
      <div class="back">
        <span class="iconfont icon-zuojiantou" @click="$router.go(-1)"></span>
        <input
          class="inputBox"
          type="text"
          :placeholder="placeholder"
          v-model="currentKeyWord"
          @keydown.enter="saveKeyWord"
        />
      </div>
    </div>

    <div class="history">
      <div class="historyLeft">历史</div>
      <div class="historyRight">
        <div class="keyWordWrapper" v-if="keyWordList.length > 0">
          <span
            v-for="(item, index) in keyWordList"
            :key="index"
            @click="traceHistory(item)"
          >
            {{ item }}
          </span>
        </div>
        <div class="iconfont icon-shanchu" @click="clearWordList"></div>
      </div>
    </div>

    <div class="playlist-main" v-if="searched">
      <div class="playItem" v-for="(item, index) in searchSongs" :key="index">
        <div class="itemLeft">
          <div class="index">{{ index + 1 }}</div>
          <div class="content">
            <div class="title">
              {{ item.name }}
            </div>
            <div class="author">
              {{ item.artists[0].name }}-{{ item.album.name }}-{{
                item.album.publishTime
              }}
            </div>
          </div>
        </div>
        <div class="itemRight">
          <span
            class="iconfont icon-bofang1"
            @click="setPlay(item, index)"
          ></span>
          <span class="iconfont icon-diandiandianshu"></span>
        </div>
      </div>
    </div>
  </div>
</template>
    
<script>
import { mapMutations } from "vuex";
import BaseApi from "@/api/index.js";
export default {
  data() {
    return {
      placeholder: "陈奕迅",
      keyWordList: [],
      currentKeyWord: "",
      searchSongs: [],
      searched: false,
    };
  },
  beforeMount() {
    if (localStorage.keyWordList) {
      this.keyWordList = JSON.parse(localStorage.keyWordList);
    }
  },
  methods: {
    saveKeyWord() {
      if (this.currentKeyWord == "") return;

      // 保存历史
      if (this.currentKeyWord != "") {
        this.keyWordList.push(this.currentKeyWord);
        // 去重
        this.keyWordList = Array.from(new Set(this.keyWordList));
        // 设置长度上限
        if (this.keyWordList.length > 10) {
          this.keyWordList = this.keyWordList.slice(
            this.keyWordList.length - 10,
            this.keyWordList.length
          );
        }
      }

      localStorage.keyWordList = JSON.stringify(this.keyWordList);

      // 搜索音乐
      BaseApi.searchMusic(this.currentKeyWord)
        .then((res) => {
          res.data.result.songs.forEach((ele) => {
            ele.album.publishTime = this.getMyDate(ele.album.publishTime);
          });
          this.searchSongs = res.data.result.songs;
        })
        .then(() => {
          this.searched = true;
        });

      console.log(this.searchSongs);
    },
    // 清空搜索记录
    clearWordList() {
      localStorage.keyWordList = [];
      this.keyWordList = [];
      this.currentKeyWord = "";
    },
    // 毫秒时间转正常
    getMyDate(str) {
      let oDate = new Date(str),
        oYear = oDate.getFullYear(),
        oMonth = oDate.getMonth() + 1,
        oDay = oDate.getDate(),
        oTime = oYear + "-" + this.addZero(oMonth) + "-" + this.addZero(oDay);
      return oTime;
    },
    addZero(num) {
      if (parseInt(num) < 10) {
        num = "0" + num;
      }
      return num;
    },
    traceHistory(songName) {
      this.currentKeyWord = songName;
      this.saveKeyWord();
    },
    setPlay(item, index) {
      console.log(item, index);
      item.al = item.album;
      item.al.picUrl = item.artists[0].img1v1Url;
      this.$store.commit("pushPlayList", item);
      this.$store.commit("setPlayIndex", this.$store.state.playList.length - 1);
    },
    ...mapMutations(["setPlayIndex"]),
  },
};
</script>
    
<style lang="less" scoped>
.ListViewTop {
  width: 7.5rem;
  // height: 6rem;
  padding: 0 0.4rem;
  .nav {
    display: flex;
    justify-content: space-between;
    align-items: center;
    height: 1.2rem;
    font-size: 0.36rem;
    .iconfont {
      font-size: 0.48rem;
    }
    .back {
      display: flex;
    }
    .icon-diandiandianshu {
      margin-left: 0.26rem;
    }
    .inputBox {
      font-size: 0.3rem;
      margin-left: 0.2rem;
      width: 6rem;
      border: none;
      border-bottom: 1px solid #ccc;
    }
  }
  .history {
    display: flex;
    justify-content: space-between;
    align-items: top;
    .historyLeft {
      color: #000;
      font-size: 0.34rem;
      font-weight: bold;
      margin-top: 0.1rem;
    }
    .historyRight {
      display: flex;
      align-items: center;
      .iconfont {
        margin-top: 0.1rem;
        font-size: 0.5rem;
      }
      .keyWordWrapper {
        display: flex;
        flex-wrap: wrap;
        width: 5rem;
        span {
          border-radius: 0.2rem;
          background: rgb(238, 238, 238);
          margin: 0.1rem;
          padding: 0.05rem 0.18rem;
        }
      }
    }
  }
  .playlist-main {
    margin: 0.2rem 0 1.5rem 0;
    .playItem {
      display: flex;
      justify-content: space-between;
      align-items: center;
      margin-bottom: 0.3rem;
      .itemLeft {
        display: flex;
        align-items: center;
        .index {
          width: 0.5rem;
          text-align: center;
          color: rgb(177, 174, 174);
          font-size: 0.4rem;
          margin-right: 0.3rem;
        }
        .title {
          // font-weight: bold;
          font-size: 0.3rem;
          line-height: 0.5rem;
        }
        .author {
          color: rgb(189, 189, 189);
          font-size: 0.22rem;
        }
      }
      .itemRight {
        .iconfont {
          font-size: 0.3rem;
        }
        .icon-bofang1 {
          margin-right: 0.36rem;
        }
      }
    }
  }
}
</style>
    